<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-10-30 12:13:43
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\ProPlan;

use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class ProPlanRepository extends Repositories
    {
    public $relation_models = [
        "material",
        "color",
        "type",
        "loom",
        "creator",
        "emergency",
        "company",
        "department",
        'pro_pot' => ['creator'],
        'process',
        'location',
        'outfactory',
        'car',
        'carry',
        'color_revision',
        'process_route'
    ];
    public function boot(ProPlan $model)
        {
        $this->model = $model;
        }
    public function getReport($ids)
        {
        $data = $this->with(['pro_pot', 'pro_yarn', 'order'])->whereIn("pro_pot_id", $ids)->where("plan_id", 1)->getList()->map(function ($item)
            {
            $data['company']['name']  = $item->company->name;
            $data['material']['name'] = $item->material->name;
            $data['material']['spec'] = $item->material->spec;
            $data['lot_name']         = $item->lot_name;
            $data['dyelot']           = $item->dyelot;
            $data['color']['name']    = $item->color->name;
            $data['color']['code']    = $item->color->code;
            $data['color']['code_en'] = $item->color->code_en;
            $data['plan_quantity']   = $item->plan_quantity;
            $data['remark']          = $item->remark;
            $data['pro_pot']['code'] = $item->pro_pot->code;
            $data['tstime']     = $item->pro_pot->tstime;
            $data['place_name'] = $item->isrepair == 1 ? "复修仓" : $item->pro_yarn->location_name;

            $data['perhank']   = $item->perhank;
            $data['pack_qtyb'] = (int) $item->auxiliary_quantity;

            $data['rscode']        = $item->pro_pot->rscode;
            $data['rsname']        = $item->pro_pot->craft->name;
            $data['rsdata']        = $item->pro_pot->craft->data;
            $data['rscodeend']     = $item->pro_pot->rscodeend;
            $data['rsendname']     = $item->pro_pot->endcraft?->name ?? "";
            $data['yarn_quantity'] = $item->yarn_quantity;
            //不是订单创建的，直接新增的都是备货
            switch (($item->order?->main->state_id) ?? 3) {
                case '1':
                    $data['state'] = '加工报染';
                    break;
                case '2':
                    $data['state'] = '销售报染';
                    break;
                case '3':
                    $data['state'] = '备货报染';
                    break;
                }
            $data['needsample'] = $item->order?->main->needsample_id == 1 ? "留样" : "";

            $data['reform_name'] = $item->isrepair == 1 ? "回修" : "";

            $data['department_name'] = $item->order?->main->department->name;

            $data['emergency'] = $item->emergency->name = !"无" ? $item->emergency->name : "";

            $data['qtybweight'] = ($item->order?->main->hg_weight) ?? 25; //烘干标准
            $data['hg_weight'] = round($data['qtybweight'] / $data['perhank'] * 10, 2) . "KG"; //10绞重

            $qtyb             = (int) $item->pro_yarn?->qtyb ?? 0;
            $hank             = (int) $item->pro_yarn?->hank ?? 0;
            $data['qtybhank'] = ($qtyb > 0 ? "{$qtyb}包" : "") . ($hank > 0 ? "{$hank}绞" : "复修无需领纱");
            // reform_name
            $data['remark']  = isEmpty($item['remark']) ? "" : "{$item['remark']};";
            $data['remark'] .= isEmpty($item['dgremark']) ? "" : "{$item['dgremark']}";
            $data['printer'] = auth()->user()->name;
            return $data;
            });
        return createReport($data);
        }
    }